#
# Makefile
#
makefile_name := $(abspath $(lastword $(MAKEFILE_LIST)))
makefile_path := $(strip $(patsubst %/,% , $(dir $(makefile_name))))

PRJ_DIR := $(makefile_path)
LVGL_DIR := $(PRJ_DIR)

CFLAGS := -O3 -g0 -I$(LVGL_DIR)/ -I$(makefile_path)/ -I$(makefile_path)/rlottie -c -fmessage-length=0
CFLAGS += -I$(LVGL_DIR)/$(LVGL_DIR_NAME)/ -I$(LVGL_DIR)/$(LVGL_DIR_NAME)/src



#CC = arm-none-linux-gnueabihf-gcc
CC = arm-linux-gnueabihf-gcc
LVGL_DIR_NAME ?= lvgl
LVGL_DIR ?= ${shell pwd}
CFLAGS ?= -O3 -g0 -I$(LVGL_DIR)/ -Wall -Wshadow -Wundef -Wmissing-prototypes -Wno-discarded-qualifiers -Wall -Wextra -Wno-unused-function -Wno-error=strict-prototypes -Wpointer-arith -fno-strict-aliasing -Wno-error=cpp -Wuninitialized -Wmaybe-uninitialized -Wno-unused-parameter -Wno-missing-field-initializers -Wtype-limits -Wsizeof-pointer-memaccess -Wno-format-nonliteral -Wno-cast-qual -Wunreachable-code -Wno-switch-default -Wreturn-type -Wmultichar -Wformat-security -Wno-ignored-qualifiers -Wno-error=pedantic -Wno-sign-compare -Wno-error=missing-prototypes -Wdouble-promotion -Wclobbered -Wdeprecated -Wempty-body -Wtype-limits -Wshift-negative-value -Wstack-usage=2048 -Wno-unused-value -Wno-unused-parameter -Wno-missing-field-initializers -Wuninitialized -Wmaybe-uninitialized -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wtype-limits -Wsizeof-pointer-memaccess -Wno-format-nonliteral -Wpointer-arith -Wno-cast-qual -Wmissing-prototypes -Wunreachable-code -Wno-switch-default -Wreturn-type -Wmultichar -Wno-discarded-qualifiers -Wformat-security -Wno-ignored-qualifiers -Wno-sign-compare
LDFLAGS ?= -lm
BIN = demo


#Collect the files to compile
MAINSRC = ./project/main/former_main.c

include $(LVGL_DIR)/lvgl/lvgl.mk
include $(LVGL_DIR)/lv_drivers/lv_drivers.mk
include ./generated/generated.mk

CFLAGS += -I$(LVGL_DIR)/ui
CFLAGS += -I$(LVGL_DIR)/ui/components
CFLAGS += -I$(LVGL_DIR)/ui/fonts
CFLAGS += -I$(LVGL_DIR)/ui/screens

VPATH  +=  :$(LVGL_DIR)/ui
VPATH  +=  :$(LVGL_DIR)/ui/images
VPATH  +=  :$(LVGL_DIR)/ui/componets
VPATH  +=  :$(LVGL_DIR)/ui/fonts
VPATH  +=  :$(LVGL_DIR)/ui/screens


INCLUDES=-I project/include

#CSRCS +=$(LVGL_DIR)/mouse_cursor_icon.c

LIBS=-L  project/lib 



LDLIBS=-lmosquitto -lssl -lcrypto -lcjson -lmodbus -lcan -lpthread -lmqtt -llogger -lconf -liniparser

UI_CSRCS += $(wildcard ui/*.c)
UI_CSRCS += $(wildcard ui/componets/*.c)
UI_CSRCS += $(wildcard ui/fonts/*.c)
UI_CSRCS += $(wildcard ui/screens/*.c)
UI_CSRCS += $(wildcard ui/images/*.c)


CSRCS += $(UI_CSRCS)
UI_OBJS := $(UI_CSRCS:.c=.o)
OBJS  += $(UI_OBJS)


OBJEXT ?= .o

AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))

MAINOBJ = $(MAINSRC:.c=$(OBJEXT))

SRCS = $(ASRCS) $(CSRCS) $(MAINSRC)
OBJS = $(AOBJS) $(COBJS)

## MAINOBJ -> OBJFILES

all: default

%.o: %.c
	@$(CC)  $(CFLAGS) -c $< -o $@ $(INCLUDES) $(LIBS)  $(LDLIBS)
	@echo "CC $<"
    
default: $(AOBJS) $(COBJS) $(MAINOBJ)
	$(CC) -o $(BIN) $(MAINOBJ) $(AOBJS) $(COBJS) $(LDFLAGS) $(INCLUDES) $(LIBS)  $(LDLIBS) 

clean: 
	rm -f $(BIN) $(AOBJS) $(COBJS) $(MAINOBJ)

